var async = require("async")
var UUID = require("uuid");
var util = require("../../../util/util.js")
module.exports = function(app) {
	return new DBRemote(app);
};

var DBRemote = function(app) {
	this.app = app
	DBRemote.app = app
    DBRemote.dbService = this.app.get("dbService")
}

var local = {}

//发邮件
DBRemote.prototype.sendMail = function(uid,title,content,award,cb){
	var curTime = (new Date()).getTime()
	var ID = UUID.v1();
	var mail = {
		title : title,
		content : content,
		award : award,
		time : curTime,
		read : false,
		received : false
	}
	if(!award){
		mail.received = true
	}
	DBRemote.dbService.db.hset("game:mail:"+uid,ID,JSON.stringify(mail),function(err) {
		if(!err){
			var notify = {
				cmd : "newMail",
				mailId : ID,
				mail : mail
			}
			DBRemote.app.rpc.game.remote.sendByUid(null,uid,notify,function(){})
		}
		if(cb){
			cb(true)
		}
	})
}
//清除过期邮件 邮件保存七天
DBRemote.prototype.clearMail = function(uid,cb) {
	DBRemote.dbService.db.hgetall("game:mail:"+uid,function(err,data) {
		if(!err && data){
			var curTime = (new Date()).getTime()
			for(var i in data){
				data[i] = JSON.parse(data[i])
				if((curTime - data[i].time) > 604800000){
					DBRemote.dbService.db.hdel("game:mail:"+uid,i)
				}
			}
		}
	})
	cb()
}
//获取邮件列表
DBRemote.prototype.getMailList = function(uid,cb) {
	local.getMailList(uid,cb)
}
//领取邮件附件
DBRemote.prototype.getMailAward = function(uid,mailId,cb) {
	console.log(uid,mailId)
	local.getMailById(uid,mailId,function(flag,mail) {
		if(!flag){
			cb(false,"邮件不存在")
			return
		}
		if(mail.received == false && mail.award){
			mail.received = true
			mail.read = true
			var mailInfo = JSON.stringify(mail)
			DBRemote.dbService.db.hdel("game:mail:"+uid,mailId,function(err,data) {
				if(!err && data){
					for(var i in mail.award){
						DBRemote.dbService.addItemReason(uid,parseInt(i),mail.award[i],"邮件:"+mail.title,function(){})
					}
					DBRemote.dbService.db.hset("game:mail:"+uid,mailId+":already",mailInfo,function(){})
					cb(true,mail.award)
				}else{
					cb(false,"未知错误")
				}
			})
		}else{
			cb(false,"已领取或无奖励")
		}
	})
}
//读邮件
DBRemote.prototype.readMail = function(uid,mailId,cb) {
	local.getMailById(uid,mailId,function(flag,mail) {
		if(!flag){
			cb(false,"邮件不存在")
			return
		}
		if(mail.read == false){
			mail.read = true
			DBRemote.dbService.db.hset("game:mail:"+uid,mailId,JSON.stringify(mail),function(err) {
				if(!err){
					cb(true)
				}else{
					cb(false,"未知错误")
				}
			})
		}
	})
}
//删除邮件
DBRemote.prototype.deletedMail = function(uid,mailId,cb) {
	local.getMailById(uid,mailId,function(flag,mail) {
		if(!flag){
			cb(false,"邮件不存在")
			return
		}
		DBRemote.dbService.db.hdel("game:mail:"+uid,mailId,function(err) {
			if(!err){
				cb(true)
			}else{
				cb(false)
			}
		})
	})
}
//清空已读
DBRemote.prototype.deletedUseless = function(uid,cb) {
	DBRemote.dbService.db.hgetall("game:mail:"+uid,function(err,data) {
		if(!err && data){
			var myList = []
			for(var i in data){
				data[i] = JSON.parse(data[i])
				if(data[i].read && data[i].received){
					myList.push(i)
					DBRemote.dbService.db.hdel("game:mail:"+uid,i)
				}
			}
			cb(true,myList)
		}else{
			cb(false)
		}
	})
}
//一键领取
DBRemote.prototype.getAllMail = function(uid,cb) {
	DBRemote.dbService.db.hgetall("game:mail:"+uid,function(err,data) {
		if(!err && data){
			var myList = []
			for(var i in data){
				data[i] = JSON.parse(data[i])
				if(data[i].received == false && data[i].award){
					myList.push(i)
					DBRemote.prototype.getMailAward(uid,i,function(){})
				}
			}
			cb(true,myList)
		}else{
			cb(false)
		}
	})
}
//获取单封邮件
local.getMailById = function(uid,mailId,cb) {
	DBRemote.dbService.db.hget("game:mail:"+uid,mailId,function(err,data) {
		if(!err && data){
			data = JSON.parse(data)
			cb(true,data)
		}else{
			cb(false)
		}
	})
}
//获取邮件列表
local.getMailList = function(uid,cb) {
	DBRemote.dbService.db.hgetall("game:mail:"+uid,function(err,data) {
		if(!err && data){
			cb(true,data)
		}else{
			cb(true,{})
		}
	})
}